home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / FastMacros.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  4KB  |  212 lines

  1. /*
  2. **    FastMacros.c
  3. **
  4. **    Fast! macros support routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     /* CreateFastMacroNode(STRPTR Macro):
  17.      *
  18.      *    Create a new fast! macro node.
  19.      */
  20.  
  21. struct MacroNode *
  22. CreateFastMacroNode(STRPTR Macro)
  23. {
  24.     struct MacroNode *Node;
  25.  
  26.     if(Node = (struct MacroNode *)AllocVecPooled(sizeof(struct MacroNode),MEMF_ANY|MEMF_CLEAR))
  27.     {
  28.         Node->Node.ln_Name = Node->Macro;
  29.  
  30.         if(Macro)
  31.             LimitedStrcpy(sizeof(Node->Macro),Node->Macro,Macro);
  32.     }
  33.  
  34.     return(Node);
  35. }
  36.  
  37.     /* SaveFastMacros(STRPTR Name):
  38.      *
  39.      *    Save the fast! macro list to a file.
  40.      */
  41.  
  42. BOOL
  43. SaveFastMacros(STRPTR Name,struct List *FastMacroList)
  44. {
  45.     struct IFFHandle *Handle;
  46.     LONG Error;
  47.  
  48.     if(!(Handle = OpenIFFStream(Name,MODE_NEWFILE)))
  49.         Error = IoErr();
  50.     else
  51.     {
  52.         if(!(Error = PushChunk(Handle,ID_TERM,ID_CAT,IFFSIZE_UNKNOWN)))
  53.         {
  54.             if(!(Error = PushChunk(Handle,ID_TERM,ID_FORM,IFFSIZE_UNKNOWN)))
  55.             {
  56.                 if(!(Error = PushChunk(Handle,0,ID_VERS,IFFSIZE_UNKNOWN)))
  57.                 {
  58.                     struct TermInfo TermInfo;
  59.  
  60.                     TermInfo.Version    = CONFIG_FILE_VERSION;
  61.                     TermInfo.Revision    = CONFIG_FILE_REVISION;
  62.  
  63.                     if(WriteChunkBytes(Handle,&TermInfo,sizeof(struct TermInfo)) != sizeof(struct TermInfo))
  64.                         Error = IoErr();
  65.                     else
  66.                         Error = PopChunk(Handle);
  67.                 }
  68.  
  69.                 if(!Error)
  70.                     Error = PopChunk(Handle);
  71.             }
  72.  
  73.             if(!Error)
  74.             {
  75.                 struct MacroNode *Node;
  76.  
  77.                 for(Node = (struct MacroNode *)FastMacroList->lh_Head ; !Error && Node->Node.ln_Succ ; Node = (struct MacroNode *)Node->Node.ln_Succ)
  78.                 {
  79.                     if(!(Error = PushChunk(Handle,ID_TERM,ID_FORM,IFFSIZE_UNKNOWN)))
  80.                     {
  81.                         if(!(Error = PushChunk(Handle,0,ID_FAST,IFFSIZE_UNKNOWN)))
  82.                         {
  83.                             if(WriteChunkBytes(Handle,Node->Macro,20) != 20)
  84.                                 Error = IoErr();
  85.                             else
  86.                             {
  87.                                 if(WriteChunkBytes(Handle,Node->Code,256) != 256)
  88.                                     Error = IoErr();
  89.                             }
  90.  
  91.                             if(!Error)
  92.                                 Error = PopChunk(Handle);
  93.                         }
  94.  
  95.                         if(!Error)
  96.                             Error = PopChunk(Handle);
  97.                     }
  98.                 }
  99.             }
  100.  
  101.             if(!Error)
  102.                 Error = PopChunk(Handle);
  103.         }
  104.  
  105.         CloseIFFStream(Handle);
  106.     }
  107.  
  108.     if(Error)
  109.     {
  110.         DeleteFile(Name);
  111.         SetIoErr(Error);
  112.  
  113.         return(FALSE);
  114.     }
  115.     else
  116.     {
  117.         AddProtection(Name,FIBF_EXECUTE);
  118.         return(TRUE);
  119.     }
  120. }
  121.  
  122.     /* LoadFastMacros(STRPTR Name):
  123.      *
  124.      *    Restore the fast! macro list from a file.
  125.      */
  126.  
  127. BOOL
  128. LoadFastMacros(STRPTR Name,struct List *FastMacroList)
  129. {
  130.     STATIC ULONG Stops[2 * 2] =
  131.     {
  132.         ID_TERM,ID_VERS,
  133.         ID_TERM,ID_FAST
  134.     };
  135.  
  136.     struct ContextNode *Chunk;
  137.     struct IFFHandle *Handle;
  138.     struct List *LocalList;
  139.     struct MacroNode *Node;
  140.     LONG Error;
  141.  
  142.     if(!(LocalList = CreateList()))
  143.         Error = IoErr();
  144.     else
  145.     {
  146.         if(!(Handle = OpenIFFStream(Name,MODE_OLDFILE)))
  147.             Error = IoErr();
  148.         else
  149.         {
  150.             if(!(Error = StopChunks(Handle,(LONG *)Stops,2)))
  151.             {
  152.                 while(!Error && !ParseIFF(Handle,IFFPARSE_SCAN))
  153.                 {
  154.                     Chunk = CurrentChunk(Handle);
  155.  
  156.                     if(Chunk->cn_ID == ID_VERS)
  157.                     {
  158.                         struct TermInfo TermInfo;
  159.  
  160.                         if(ReadChunkBytes(Handle,&TermInfo,sizeof(struct TermInfo)) != sizeof(struct TermInfo))
  161.                             Error = IoErr();
  162.                         else
  163.                         {
  164.                             if((TermInfo.Version > CONFIG_FILE_VERSION) || (TermInfo.Version == CONFIG_FILE_VERSION && TermInfo.Revision > CONFIG_FILE_REVISION) || (TermInfo.Version == 1 && TermInfo.Revision < 6))
  165.                                 Error = ERR_OUTDATED;
  166.                         }
  167.                     }
  168.  
  169.                     if(!Error && Chunk->cn_ID == ID_FAST)
  170.                     {
  171.                         if(!(Node = CreateFastMacroNode(NULL)))
  172.                             Error = ERROR_NO_FREE_STORE;
  173.                         else
  174.                         {
  175.                             if(ReadChunkBytes(Handle,Node->Macro,20) != 20)
  176.                                 Error = IoErr();
  177.                             else
  178.                             {
  179.                                 if(ReadChunkBytes(Handle,Node->Code,256) != 256)
  180.                                     Error = IoErr();
  181.                                 else
  182.                                     AddTail(LocalList,(struct Node *)Node);
  183.                             }
  184.                         }
  185.                     }
  186.                 }
  187.  
  188.                 if(!Error && !IsListEmpty(LocalList))
  189.                 {
  190.                     FreeList(FastMacroList);
  191.  
  192.                     MoveList(LocalList,FastMacroList);
  193.  
  194.                     FastMacroCount = GetListSize(FastMacroList);
  195.                 }
  196.             }
  197.  
  198.             CloseIFFStream(Handle);
  199.         }
  200.  
  201.         DeleteList(LocalList);
  202.     }
  203.  
  204.     if(Error)
  205.     {
  206.         SetIoErr(Error);
  207.         return(FALSE);
  208.     }
  209.     else
  210.         return(TRUE);
  211. }
  212.